<template>
	<div class="exercises-test exercises-test-preview" v-loading.fullscreen.lock="loading"
		element-loading-text="拼命加载中">
        <el-row class="preview-head" type="flex" align="middle">
            <el-col :span="2" :offset="1">
                <el-button type="text" @click="goBack()"><i class="el-icon-arrow-left"></i>返回</el-button>
            </el-col>
        </el-row>

		<div class="preview-conten maxW">
			
			<div class="preview-conten-l">
				
				<div class="preview-conten-l-h">
					<h3>{{testList.name}}</h3>
					<!-- <p>考试时间：120分钟，满分{{this.totalScore}}分</p> -->
					<p>考试时间：{{testList.answer_time ? testList.answer_time : '--'}} 分钟，满分{{totalScore}}分</p>
					
				</div>

				<ul>
					<template v-for="(type, list) of testList.question_type" >
						<li class="preview-conten-list" :key="type.id">
							<div class="preview-conten-title" >
								{{type.cn_num}}. {{type.question_type}} (本类题共{{type.count}}小题，共{{type.score}}分<span v-show="type.common">，{{type.common}}。</span>)
							</div>
							
							<!-- <div class="preview-conten-title" v-if="type.id == 1">
								{{type.cn_num}}. {{type.question_type}} (本类题共{{type.count}}小题，每小题{{type.score/type.count}}分，共{{type.score}}分，每小题备选答案中，只有一个符合题意的正确答案。错选，不选均不得分。)
							</div>

							<div class="preview-conten-title"  v-else-if="type.id == 2">
								{{type.cn_num}}. {{type.question_type}} (本类题共{{type.count}}小题，每小题{{type.score/type.count}}分，共{{type.score}}分，每小题备选答案中，有两个或两个以上符合题意的正确答案。多选、少选、错选、不选均不得分。)
							</div>

							<div class="preview-conten-title"  v-else-if="type.id == 9">
								{{type.cn_num}}. {{type.question_type}} (本类题共{{type.count}}小题，每小题{{type.score/type.count}}分，共{{type.score}}分，每小题备选答案中，两个答案中只有一个符合题意的正确答案。)
							</div>
							<div class="preview-conten-title"  v-else>
								{{type.cn_num}}. {{type.question_type}} (本类题共{{type.count}}小题，每小题{{type.score/type.count}}分，共{{type.score}}分，点击文本框，直接在文本框内作答即可。)
							</div>
 -->
							<ul class="preview-conten-subject">

								<template v-for="(item, index) of testList.question_list" v-if="parseInt(item.qt_id) === parseInt(type.id)">
									<li :id="item.id" class="preview-conten-subject-c"  v-if="parseInt(item.type_base_id) === 1 ">

										<template>
											<div  v-if="item.pattern === 1" class="preview-conten-subject-t">
												<i>{{index+1}}. </i>
												<span v-html="item.content"></span>
												<strong>({{item.score}}分)</strong>
											</div>
											<div  v-if="item.pattern === 2" class="preview-conten-subject-t">
												<i>{{index+1}}. </i>
												<span v-html="item.content"></span>
												<strong>({{item.score}}分)</strong>
											</div>
										</template>
										
										<el-radio-group v-model="answerList[index].answer[0]"  @change="selectAnswerChange($event, index, item, '1', 1)">
											<ul class="preview-topic">
												<template v-for="(option,key) in item.option">
													
													<li class="preview-subject-title" v-if="item.pattern === 1">
														<el-radio :label="option.name"></el-radio>
														<div class="" v-html="option.value"></div>
													</li>

													<li class="preview-subject-title option-pattern" v-if="item.pattern === 2">
														<el-radio :label="option.name"></el-radio>
														<!-- <div class="" v-html="option.value"></div> -->
													</li>
												</template>
												
											</ul>
										</el-radio-group>
											

										<el-collapse accordion class="preview-answer" v-if='0'>
											<el-collapse-item>
												<template slot="title">
													<div class="preview-accordion-title">
														<span>题型：{{item.question_type}}</span>
														<span>难度： {{item.difficulty}}</span>
														<span v-if="item.keynote == 1">重点程度： 低</span>
														<span v-if="item.keynote == 2">重点程度： 中</span>
														<span v-if="item.keynote == 3">重点程度： 高</span>
													</div>
												</template>
												<div class="preview-accordion-answer">
													<span>正确答案： </span>
													<div>
														<i v-for="answer of item.answer">{{answer}}</i>
													</div>
												</div>
												<div class="preview-accordion-answer">
													<span>统计： </span>
													<div>本题共被作答 {{item.question_info.answer_count}} 次，正确率为 {{item.question_info.true_probability}}%</div>
												</div>
												<div class="preview-accordion-answer">
													<span>解析： </span>
													<template v-if="item.pattern == 1">
														<div v-html="item.resolve"></div>
													</template>
													<template v-if="item.pattern == 2">
														<div>
															<img :src="BASE_URL + item.resolve_path">
														</div>
													</template>
												</div>
											</el-collapse-item>
										</el-collapse>
									</li>

									<li :id="item.id" class="preview-conten-subject-c"  v-else-if="parseInt(item.type_base_id) === 2">

										<template>
											<div  v-if="item.pattern === 1" class="preview-conten-subject-t">
												<i>{{index+1}}. </i>
												<span v-html="item.content"></span>
												<strong>({{item.score}}分)</strong>
											</div>
											<div  v-if="item.pattern === 2" class="preview-conten-subject-t">
												<i>{{index+1}}. </i>
												<span v-html="item.content"></span>
												<strong>({{item.score}}分)</strong>
											</div>
										</template>
										
										<ul class="preview-topic">

											<template  v-for="(option,key) in item.option">
												<li class="preview-subject-title" v-if="item.pattern === 1">
													<el-checkbox 
													@change="selectAnswerChange($event, index, item, option.name, 2)" 
													:label="option.name"></el-checkbox>
													<div class="" v-html="option.value"></div>
												</li>

												<li class="preview-subject-title option-pattern" v-if="item.pattern === 2">
													<el-checkbox 
													@change="selectAnswerChange($event, index, item, option.name, 2)" 
													:label="option.name"></el-checkbox>
													<!-- <div class="" v-html="option.value"></div> -->
												</li>
											</template>
											
										</ul>
											

										<el-collapse accordion class="preview-answer" v-if='0'>
											<el-collapse-item>
												<template slot="title">
													<div class="preview-accordion-title">
														<span>题型：{{item.question_type}}</span>
														<span>难度： {{item.difficulty}}</span>
														<span v-if="item.keynote == 1">重点程度： 低</span>
														<span v-if="item.keynote == 2">重点程度： 中</span>
														<span v-if="item.keynote == 3">重点程度： 高</span>
													</div>
												</template>
												<div class="preview-accordion-answer">
													<span>正确答案： </span>
													<div>
														<i v-for="answer of item.answer">{{answer}}</i>
													</div>
												</div>
												<div class="preview-accordion-answer">
													<span>统计： </span>
													<div>本题共被作答 {{item.question_info.answer_count}} 次，正确率为 {{item.question_info.true_probability}}%</div>
												</div>
												<div class="preview-accordion-answer">
													<span>解析： </span>
													<template v-if="item.pattern == 1">
														<div v-html="item.resolve"></div>
													</template>
													<template v-if="item.pattern == 2">
														<div>
															<img :src="BASE_URL + item.resolve_path">
														</div>
													</template>
												</div>
											</el-collapse-item>
										</el-collapse>
									</li>

									<li :id="item.id" class="preview-conten-subject-c"  v-else-if="parseInt(item.type_base_id) === 4">

										<template>
											<div  v-if="item.pattern === 1" class="preview-conten-subject-t">
												<i>{{index+1}}. </i>
												<span v-html="item.content"></span>
												<strong>({{item.score}}分)</strong>
											</div>
											<div  v-if="item.pattern === 2" class="preview-conten-subject-t">
												<i>{{index+1}}. </i>
												<span v-html="item.content"></span>
												<strong>({{item.score}}分)</strong>
											</div>
										</template>
										
			

										<el-radio-group v-model="answerList[index].answer[0]" @change="selectAnswerChange($event,  index, item, '9', 9)">
											
											<ul class="preview-topic">
												<template v-for="(option,key) in item.option">
													
													<li class="preview-subject-title" v-if="item.pattern === 1">
														<el-radio :label="option.name" ></el-radio>
														<!-- <div class="" v-html="option.value"></div> -->
													</li>

													<li class="preview-subject-title option-pattern" v-if="item.pattern === 2">
														<el-radio :label="option.name"></el-radio>
														<!-- <div class="" v-html="option.value"></div> -->
													</li>
												</template>
												
											</ul>
										</el-radio-group>
											

										<el-collapse accordion class="preview-answer" v-if='0'>
											<el-collapse-item>
												<template slot="title">
													<div class="preview-accordion-title">
														<span>题型：{{item.question_type}}</span>
														<span>难度： {{item.difficulty}}</span>
														<span v-if="item.keynote == 1">重点程度： 低</span>
														<span v-if="item.keynote == 2">重点程度： 中</span>
														<span v-if="item.keynote == 3">重点程度： 高</span>
													</div>
												</template>
												<div class="preview-accordion-answer">
													<span>正确答案： </span>
													<div>
														<i v-for="answer of item.answer">{{answer}}</i>
													</div>
												</div>
												<div class="preview-accordion-answer">
													<span>统计： </span>
													<div>本题共被作答 {{item.question_info.answer_count}} 次，正确率为 {{item.question_info.true_probability}}%</div>
												</div>
												<div class="preview-accordion-answer">
													<span>解析： </span>
													<template v-if="item.pattern == 1">
														<div v-html="item.resolve"></div>
													</template>
													<template v-if="item.pattern == 2">
														<div>
															<img :src="BASE_URL + item.resolve_path">
														</div>
													</template>
												</div>
											</el-collapse-item>
										</el-collapse>
									</li>

									<li :id="item.id" class="preview-conten-subject-c"   v-else-if="parseInt(item.qt_id) === 3"  :key="index">

										<template>
											<div  v-if="item.pattern === 1" class="preview-conten-subject-t">
												<i>{{index+1}}. </i>
												<span v-html="item.content"></span>
												<strong v-if="item.score">({{item.score}}分)</strong>
											</div>
											<div  v-if="item.pattern === 2" class="preview-conten-subject-t">
												<i>{{index+1}}. </i>
												<span v-html="item.content"></span>
												<strong>({{item.score}}分)</strong>
											</div>
										</template>
										
										<div class="knowledge-answer">
											<span>答案</span>
											<div class="knowledge-">
												<template v-for="num of item.answerArr">

													<textarea class="knowledge-textarea"  @input="selectAnswerChange($event,  index,  item, num.answer, 3)"
													 rows="2"  v-model.trim.native="num.answer" placeholder="请输入内容, 300个字符内" maxlength="300"></textarea>
												</template>

												<!-- <vue-editor :id="`answer-${index}`"  v-if='0'
												:editorToolbar="customToolbar"
												 placeholder="请输入内容"
												 @selection-change="selectAnswerChange($event, list, index, 1 , item.id, item.answer, 3)"
												 v-model="item.answer"></vue-editor> -->
											</div>
										</div>

										<el-collapse accordion class="preview-answer" v-if='0'>
											<el-collapse-item>
												<template slot="title">
													<div class="preview-accordion-title">
														<span>题型：{{item.question_type}}</span>
														<span>难度： {{item.difficulty}}</span>
														<span v-if="item.keynote == 1">重点程度： 低</span>
														<span v-if="item.keynote == 2">重点程度： 中</span>
														<span v-if="item.keynote == 3">重点程度： 高</span>
													</div>
													
												</template>
												<div class="preview-accordion-answer">
													<span>正确答案： </span>
													<div>
														<i v-for="answer of item.answer">{{answer}}</i>
													</div>
												</div>
												<div class="preview-accordion-answer">
													<span>统计： </span>
													<div>本题共被作答 {{item.question_info.answer_count}} 次，正确率为 {{item.question_info.true_probability}}%</div>
												</div>
												<div class="preview-accordion-answer">
													<span>解析： </span>
													<template v-if="item.pattern == 1">
														<div v-html="item.resolve"></div>
													</template>
													<template v-if="item.pattern == 2">
														<div>
															<img :src="BASE_URL + item.resolve_path">
														</div>
													</template>
												</div>
											</el-collapse-item>
										</el-collapse>
									</li>

									<li :id="item.id" class="preview-conten-subject-c"  v-else>

										<template>
											<div  v-if="item.pattern === 1" class="preview-conten-subject-t">
												<i>{{index+1}}. </i>
												<span v-html="item.content"></span>
												<strong>({{item.score}}分)</strong>
											</div>
											<div  v-if="item.pattern === 2" class="preview-conten-subject-t">
												<i>{{index+1}}. </i>
												<span v-html="item.content"></span>
												<strong>({{item.score}}分)</strong>
											</div>
										</template>

										<div class="knowledge-answer">
											<span>答案</span>
											<div class="knowledge-">

												<Ueditor  v-model="item.answer" :writeMsg="item.answer" 
												 :id="'ueditor1'+(index+1)"  :config="config1"  :ref="'option'+(index+1)" 
												 @input="selectAnswerChange($event, index, item, '0', 0)"></Ueditor> 

                                                <!-- <vue-ueditor-wrap :config="config1"   
                                                :destroy="true"  
                                                :ref="'option'+(index+1)"
                                                v-model="item.answer" 
                                                class="Ueditor-txt" 
                                                :id="'ueditor1'+(index+1)"
                                                @input="selectAnswerChange($event, index, item, '0', 0)"></vue-ueditor-wrap> -->

											</div>
										</div>
											

										<el-collapse accordion class="preview-answer" v-if='0'>
											<el-collapse-item>
												<template slot="title">
													<div class="preview-accordion-title">
														<span>题型：{{item.question_type}}</span>
														<span>难度： {{item.difficulty}}</span>
														<span v-if="item.keynote == 1">重点程度： 低</span>
														<span v-if="item.keynote == 2">重点程度： 中</span>
														<span v-if="item.keynote == 3">重点程度： 高</span>
													</div>
												</template>
												<div class="preview-accordion-answer">
													<span>正确答案： </span>
													<div>
														<i v-for="answer of item.answer">{{answer}}</i>
													</div>
												</div>
												<div class="preview-accordion-answer">
													<span>统计： </span>
													<div>本题共被作答 {{item.question_info.answer_count}} 次，正确率为 {{item.question_info.true_probability}}%</div>
												</div>
												<div class="preview-accordion-answer">
													<span>解析： </span>
													<template v-if="item.pattern == 1">
														<div v-html="item.resolve"></div>
													</template>
													<template v-if="item.pattern == 2">
														<div>
															<img :src="BASE_URL + item.resolve_path">
														</div>
													</template>
												</div>
											</el-collapse-item>
										</el-collapse>
									</li>
								</template>

							</ul>
						</li>
					</template>
				</ul>

			</div>

			<div class="exercises-conten-r">

				<div class="exercises-conten-time" v-if="0">
					<div class="exercises-time-countdown">
						<i class="el-icon-time"></i>
						<strong>剩余时间</strong>
						<span>{{testObj.time?testObj.time: '00:00:00'}}</span>
					</div>
				</div>

				<ul class="exercises-conten-r-type">

					<template  v-for="(type, list) of questionList" >
						<li class="exercises-conten-r-type-state">
							<div>{{type.name}}<i>（共{{type.score}}分）</i></div>
							<ul>
								<template  v-for="(item, index) of type.children">
									<li  v-if="parseInt(item.qt_id) == parseInt(type.id)" :key="index" >
										<input type="checkbox" :name="index" :checked="item.html_default">
										<a :href=" '#' + item.id">{{++index}}</a>
									</li>
								</template>
							</ul>
						</li>
					</template>
						
				</ul>

				<div class="exercises-answer-state">
					<span>已答<i></i></span>
					<span>未答<i></i></span>
				</div>

				
				<div class="exercises-conten-r-btn">
					<el-button type="primary" @click="setSubmit()">交卷</el-button>
				</div>

			</div>

		</div>

		<!-- 预览图片 -->
		<image-viewer 
            v-if="imagePreview.showViewer"
            :initial-index="imageIndex"
            :on-close="closeViewer"
            :url-list="imagePreview.previewSrcList">
        </image-viewer>
	</div>
</template>

<script>
	
	// import { VueEditor } from "vue2-editor";
	// import tinymce from "vue-tinymce-editor";
	// import CN from "./../../../assets/tinymce_languages/zh_CN.js";
	// import Ueditor from '@/components/common/Ueditor.vue';
	import VueUeditorWrap from 'vue-ueditor-wrap' // ES6 Module
	import Ueditor from '@/components/common/Ueditor.vue';
	export default {
		components: {Ueditor,VueUeditorWrap},
		data(){
			return {
				imagePreview: {/*图片预览*/
					showViewer: false,
					previewSrcList: []
				},
				headClassList: null,
				questionList: [], 	/*试卷列表*/
				testList: [],
				answerList: [],
				totalScore: 0,
				testObj: {
					name: '',
					time: '',
					time2: '',
					score: 0,
					type: 1,
				},
				isPraise: 0, 		/*0.未批，1.已改*/
				recordId: '',		/*试卷id*/
				answerInfo: {
					count: 0,		/*系统改卷-全部题目*/
					score: 0, 		/*系统改卷-正确题目*/
					correct: 0, 	/*系统改卷-得分*/
					sum: 0,			/*主观题--全部题目*/
					score: 0		/*主观题--得分*/
				},
				
	
				// plugins: ["paste", "fullscreen", "link", "image"],
				// toolbar1: "",
				// toolbar2:
				//   "formatselect  | fontsizeselect | forecolor backcolor | fontselect bold italic underline strikethrough | alignleft aligncenter alignright alignjustify | outdent indent | removeformat",
				// options: {
				// 	images_upload_handler: this.images_upload_handler,
				// 	language_url: CN,
				// 	images_upload_url: this.BASE_URL + "/api/resource/res_upload_permanent",
				// 	paste_data_images: true, // 粘贴的同时能把内容里的图片自动上传，非常强力的功能
				// 	paste_convert_word_fake_lists: false, // 插入word文档需要该属性
				// 	paste_webkit_styles: "all",
				// 	paste_merge_formats: true,
				// 	nonbreaking_force_tab: false,
				// 	paste_auto_cleanup_on_paste: false
				// },
				loading: true,
				imgVisible: false,
				imgArray: [],
				imgIndex: 0,

	    		customToolbar: [
    		        [{ list: "ordered" }, { list: "bullet" }],
    		        [{ 'color': [] }, { 'background': [] }], 
    		        ['clean']
	    		],

		        config1: {
		        	zIndex: 3,
		        	textIndent: 0,
		        	maximumWords: 2000,
		        	elementPathEnabled: false,/*元素路径*/
		        	enableAutoSave: false,
					autoHeightEnabled: false,
					autoFloatEnabled: false,//不允许滚动时固定在顶部
					initialContent:'请输入内容...',   //初始化编辑器的内容,也可以通过textarea/script给值，看官网例子
					autoClearinitialContent:true, //是否自动清除编辑器初始内容，注意：如果focus属性设置为true,这个也为真，那么编辑器一上来就会触发导致初始化的内容看不到了
					initialFrameWidth: null,
					initialFrameHeight: 150,
					serverUrl: this.BASE_URL+"/static/_/UEditor/1.4.3//php/controller.php?action=uploadimage",
					BaseUrl: this.BASE_URL,
					// UEDITOR_HOME_URL: process.env.BASE_URL+'ueditor/',
					UEDITOR_HOME_URL: process.env.BASE_URL ? process.env.BASE_URL + 'ueditor/' : '/static/ueditor/',
					toolbars: [
						['bold', 'italic', 'underline',  'strikethrough', 'superscript', 'subscript', 'removeformat', 'formatmatch', 'pasteplain',  'forecolor', 'backcolor', 'insertorderedlist', 'insertunorderedlist', 'selectall', 'cleardoc','fullscreen',  'simpleupload','link', 'justifyleft', 'justifyright', 'justifycenter', 'lineheight', 'fontfamily', 'fontsize', 'undo', 'redo']
					]
		        },

			}
		},
		mounted(){
			this.headClassList = document.querySelector('#trunk').classList;
			this.headClassList.add('hidden-header')

			document.querySelector('.preview-conten-l').addEventListener('click',  (e)=> {
				
				let [targetSrc ,imgs, srcArr, currentIdx] = ['', '', '', '']
				if (e.target.nodeName === 'IMG') {

					this.imagePreview.showViewer = true
					targetSrc = e.target.src;
					imgs = [...e.target.parentNode.children];
					srcArr = imgs.map(item=>item.currentSrc);
					currentIdx = srcArr.indexOf(targetSrc);

					this.imagePreview.previewSrcList = srcArr
					this.imgIndex = targetSrc

				}
			
			}, false)
		},
		computed: {
			imageIndex() {
			    return this.imagePreview.previewSrcList.indexOf(this.imgIndex);
			}
		},
		destroyed(){
			this.headClassList&&this.headClassList.remove('hidden-header')
		},
		created(){
			this.getTestList()
		},
		methods: {
			/**
			 * 关闭图片
			 * @DateTime 2020-04-16T17:40:41+0800
			 * @qiu_m
			 * @return   {[type]}                 [description]
			 */
			 closeViewer() {
			 	this.imagePreview.showViewer = false;
			 	this.imagePreview.previewSrcList = []
			 },
			/**
			 * 根据参数请求试卷详情
			 * @DateTime 2019-07-22T17:37:30+0800
			 * @return   {[type]}                 [description]
			 */
			getTestList(){
				let success = (res=>{

					this.testList = res.data;
					// 计算试卷的总分数
					for (let s = 0; s < this.testList.question_type.length; s++) {
						this.totalScore += parseInt(this.testList.question_type[s].score)
					};

					/*根据 id 升序排序试卷*/
					this.testList.question_list = this.testList.question_list.sort(this.reSort('qt_id'));


					// this.renderTimeuse(this.testObj.time)
					this.testObj.name = res.data.task_name;

					/*组装一个新的试卷数据*/
					this.questionList = [];
					res.data.question_type.forEach((item)=>{
						this.questionList.push({
							id: item.id,
							count: item.count,
							name: item.question_type,
							score: item.score,
							type: item.cn_num,
							children: []
						});
					})

					res.data.question_list.forEach((list, key)=>{
					
						/*把试题答案提出，放进一个新数组*/

						if (list.qt_id == 3) {

								let count = 0;
								let answerArr = []
								for (var i = 0; i < list.blank_num; i++) {
						
									answerArr.push({'answer': ''})
								}
								list.answerArr = [];
								this.$set(Object.assign( list.answerArr,  answerArr))
								// count++;
						}
						
						// if (list.qt_id == 1 || list.qt_id == 2 || list.qt_id == 9) {
						// 	this.answerList.push({
						// 		question_id: list.id,
						// 		answer: []
						// 	})
						// }else{
						// 	list.answer = '';
						// 	this.answerList.push({
						// 		question_id: list.id,
						// 		answer: null
						// 	})
						// }

						this.questionList.forEach((item, index)=>{
							if (list.qt_id == item.id) {
								item.children.push(list)

							}
						});

						/*把试题答案提出，放进一个新数组*/
						this.answerList.push({
							type: list.type_base_id,
							question_id: list.id,
							answer: []
						})

					});
/*
					this.$nextTick(()=>{
						//改变试题 img 的 url
						let imgArr = document.querySelectorAll('.preview-conten-subject-c img')
						let baseUrl = sessionStorage.getItem('host_addr');
						for (var i = 0; i < imgArr.length; i++) {
							let _temp = imgArr[i].src
							imgArr[i].src = _temp.replace(location.protocol+'//'+location.host, baseUrl);
						}
					})*/

					this.loading = false
				});

				let params = {
					user_id: this.$g.user_id,
					id: this.$route.query.id
				};

				let fail = (res=>{
					this.$message.error({
						duration: 8000,
						message: res.msg
					})
				})

				this.$ajax({
					url: 'api/newtest/test_detail',
					method: 'get',
					params: params,
					func: {success: success, fail: fail}
				})
			},
			/**
			 * 重新排序试卷
			 * @DateTime 2019-08-12T09:38:37+0800
			 * @param    {[type]}                 arr   [description]
			 * @return   {[type]}                       [description]
			 */
			 reSort(arr){

			 	return function(a,b){
			 		var value1 = a[arr];
			 		var value2 = b[arr];
			 		return value1 - value2;
			 	}
			 },
			renderTimeuse(time){
				var t = parseInt(time*1000)
				 if(t > 0){
				    let day = Math.floor(t / 86400000);
				    let hour=Math.floor((t / 3600000) % 24);
				    let min=Math.floor((t / 60000) % 60);
				    let sec=Math.floor((t / 1000) % 60);
				    hour = hour < 10 ? "0" + hour : hour;
				    min = min < 10 ? "0" + min : min;
				    sec = sec < 10 ? "0" + sec : sec;
				    let format = '';
				    if(day > 0){
				      format = `${day}:${hour}:${min}:${sec}`;
				    } 
				    if(day <= 0 && hour > 0 ){
				      format = `${hour}:${min}:${sec}`; 
				    }
				    if(day <= 0 && hour <= 0){
				      format =`${hour}:${min}:${sec}`;
				    }

				    this.testObj.time = format;
				  }
			},
			/**
			 * 选择的答案
			 * @DateTime 2019-08-22T10:14:19+0800
			 * @param    {[stong]}       selected    [选中的答案]
			 * @param    {[stong]}       list        [题型的索引]
			 * @param    {[stong]}       index       [试题的索引]
			 * @param    {[stong]}       key         [选择题的索引]
			 * @param    {[stong]}       id          [试题的 id]
			 * @param    {[stong]}       answer      [选择题的选项]
			 * @param    {[stong]}       type        [1. 单选题， 2.多选， 3.其他]
			 * @return   {[type]}                             [description]
			 */
			selectAnswerChange( selected ,index,  item, name, type ){

				switch(type){
					case 1:/*多选题*/
						item.html_default = true;
					break;
					case 2:/*多选题*/

						if( is_empty(this.answerList[index].answer) ){
							this.answerList[index].answer = [];
						}
						if( selected ){
							if( !this.answerList[index].answer.includes( name) ){
								this.answerList[index].answer.push( name);
							}
						}else{
							if(this.answerList[index].answer.includes( name) ){
								this.answerList[index].answer.splice( this.answerList[index].answer.indexOf(name) , 1);
							}
						}
						/*ABCD排序*/
						this.answerList[index].answer.sort(function(a, b) {
							return a.localeCompare(b)
						})

						if (this.answerList[index].answer.length === 0) {
							item.html_default = false;
						}else{
							item.html_default = true;
						}
						
					break;
					case 3:/*填空题*/

					if (!is_empty(name)) {
						item.html_default = true;
					}else{
						item.html_default = false;
					}
					break;
					case 9:/*判断题*/
						item.html_default = true;
					break;
					case 0:/*其他*/
						console.log(item)
						if (!is_empty(item.answer)) {
							item.html_default = true;
							this.answerList[index].answer = item.answer
						}else{
							item.html_default = false;
							this.answerList[index].answer = ''
						}
						
					break;
				}
				
			},
			/**
			 * 试卷提交数据
			 * @DateTime 2019-08-22T16:34:15+0800
			 * @return   {[type]}                 [description]
			 */
			submitTest(){
				// if (this.answerStatus == 1) {return this.$message.error('你已经提交了试卷，不能重复提交');}

				let success = (res=>{
					this.$message.success(res.msg);
					setTimeout( () => {
						this.$router.push({path: '/exercises/library-result', query: {id: this.$route.query.id}})
					}, 1000);
				});


				let params = {
					test_id: this.$route.query.id,
					userid: this.$g.user_id,
					question_list: this.answerList,
				};

				this.$ajax.post('api/newtest/add_user_test_library',params,{func: {success: success}})
			},
			/**
			 * 发布任务表单
			 * @return {[type]} [description]
			 */
			setSubmit(){
				// console.log(this.questionList);
				/*检查用户的试题是否全部完成，给出不同的文字提示*/
				/*检查用户的试题是否全部完成，给出不同的文字提示*/
				let [txt, stutas] = ['', 'success']
				for (let i = 0; i < this.questionList.length; i++) {
					if (this.questionList[i].id == 3 ) {
				
						for (let j = 0; j < this.questionList[i].children.length; j++) {
							let item = this.questionList[i].children[j]

							for (let k = 0; k < item.answerArr.length; k++) {
								
								for (let a = 0; a < this.answerList.length; a++) {

									if (this.answerList[a].question_id == item.id ) {
										
										this.answerList[a].answer = item.answerArr
									}

									
								}
							}
							
						}
					}
				}

				/*检查试题是否有没有作答*/
				for (let i = 0; i < this.answerList.length; i++) {

					if (stutas =='error') {
						break;
					}

					/*填空题是多个答案*/
					if (this.answerList[i].type == 3) {
						
						let cont = []
						for (let j = 0; j < this.answerList[i].answer.length; j++) {
							if ( this.answerList[i].answer[j].answer == '') {
								txt = '还有题目未回答，是否提交试卷?'
								stutas = 'error'
								break;
							}else{

								txt = '确认要提交试卷吗？';
								stutas = 'success'
							}

						}
					}else{
						if (this.answerList[i].answer.length == 0 ) {
							txt = '还有题目未回答，是否提交试卷?'
							stutas = 'error'
							break;
						}else{
							txt = '确认要提交试卷吗？';
							stutas = 'success'
						}
					}
				}


				this.$confirm(txt, '提示', {
				   confirmButtonText: '确认',
				   cancelButtonText: '取消',
				    type: stutas,
				    customClass: 'myHomework',
				}).then(() => {

					this.submitTest()
				    
				}).catch(() => {});
			},

			 /**
			  * 返回上一页
			  * @return {[type]} [description]
			  */
			 goBack(){

			 	this.$confirm('返回上一页，数据不作保存', '提示', {
			 		confirmButtonText: '确定',
			 		cancelButtonText: '取消',
			 		type: 'warning',
			 		customClass: 'confirm-them',
			 	}).then(() => {
			 		// this.$route.back(-1)
			 		this.$router.push({path: '/exercises/volume-library'})

			 	}).catch(() => {});

			 },
			/**val 参数是选中的试题题目
			 * 点击选中的试题题目
			 */
		}
	}
</script>
